package com.example.beancopy;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.junit.Test;
import org.mapstruct.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 *
 * @author qingyan 2021-11-10
 */
public class CpoyTest {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Student implements Cloneable{

        private String name;
        private String sex;
        private String age;
        private String school;
        private String clazz;
        private String address;

        private Student student;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    static Student student;

    static {
        student = new Student("张三","男","20","北京小学","6班","地球村",null);
        student.setStudent(new Student("李四", "男", "25", "北京小学", "6班", "地球村", null));
    }

    int size = 10000;


    @SneakyThrows
    @Test
    public void test() {
        for (int i = 0; i < size; i++) {
            Student clone = (Student)student.clone();
        }

    }

    @SneakyThrows
    @Test
    public void test1(){
        for (int i = 0; i < size; i++) {
            Student student = Student.class.newInstance();
            BeanCopier copier = BeanCopier.create(Student.class, Student.class, false);
            copier.copy(this.student, student, null);
        }
    };

    @Test
    public void test2(){
        for (int i = 0; i < size; i++) {
            Student student = BeanUtil.copyProperties(this.student, Student.class);
        }
    };
    @Test
    public void test3(){
        for (int i = 0; i < size; i++) {
            Student student = new Student();
            BeanUtils.copyProperties(this.student, student);
        }
    }

    @Test
    public void test4(){
        for (int i = 0; i < size; i++) {
            Map<String, Object> map = new LinkedHashMap<>();
            BeanUtils.copyProperties(this.student, map);
            System.out.println(map);
        }
    }
    @Test
    public void test5() throws CloneNotSupportedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Object clone = student.clone();
        Student student1 = new Student();
        BeanUtils.copyProperties(student, student1);
        Student student2 = BeanUtil.copyProperties(student, Student.class);
        Student student3 = new Student();
        BeanCopier beanCopier = BeanCopier.create(Student.class, Student.class, false);
        beanCopier.copy(student, student3, null);
        Object cloneBean = org.apache.commons.beanutils.BeanUtils.cloneBean(student2);
        System.out.println(student);
        System.out.println(clone);
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student3);
        System.out.println(cloneBean);
    }



}
